/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
*/
package com.jorritdevries.console.elements
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.filters.DropShadowFilter;
	import flash.utils.getDefinitionByName;


	/** 
	 * Cursor used when hovering over the resize areas. These areas consist of
	 * the right side to enlarge the width to the right, the bottom to enlarge
	 * the height in the lower direction, the left to enlarge the width to the
	 * left, the top to enlarge the height in the upper direction and the
	 * bottomright corner to enlarge in the right & lower direction at once.
	 */
	public class Cursor extends MovieClip
	{
		public static const BOTTOM_TO_TOP:int = 0;
		public static const TOP_TO_BOTTOM:int = 1;
		public static const HORIZONTAL:int    = 2;
		public static const VERTICAL:int      = 3;
		
		private var mBitmaps:Array;
		
		private static var msInstance:Cursor = null;
		
		private var mShadowFilter:DropShadowFilter;
		
		public function Cursor(enforcer:SingletonEnforcer)
		{
			if (!enforcer)
			{
				// Throw exception
			}
			
			// Load bitmaps
			mBitmaps = new Array();
			var SizeOne:Class = getDefinitionByName("com.jorritdevries.console.skin.SizeOne") as Class;
			var SizeTwo:Class = getDefinitionByName("com.jorritdevries.console.skin.SizeTwo") as Class;
			var SizeThree:Class = getDefinitionByName("com.jorritdevries.console.skin.SizeThree") as Class;
			var SizeFour:Class = getDefinitionByName("com.jorritdevries.console.skin.SizeFour") as Class;
			mBitmaps.push(new Bitmap(new SizeOne(17, 17) as BitmapData));
			mBitmaps.push(new Bitmap(new SizeTwo(17, 17) as BitmapData));
			mBitmaps.push(new Bitmap(new SizeThree(24, 23) as BitmapData));
			mBitmaps.push(new Bitmap(new SizeFour(23, 23) as BitmapData));
			
			// Disable mouse events for the cursor
			mouseEnabled = false;
			mShadowFilter = new DropShadowFilter(2.5, 30, 0xAAAAAA, 1, 4, 4, 1.5);
			filters = [mShadowFilter];
			visible = false;
		}
		
		public static function getInstance():Cursor
		{
			if (!msInstance)
			{
				msInstance = new Cursor(new SingletonEnforcer);
			}
			return msInstance;
		}
		
		public function useBitmap(index:uint):void
		{
			if (numChildren > 0)
			{
				removeChildAt(0);
			}
			// Note we do no index assertion!
			var bitmap:Bitmap = Bitmap(mBitmaps[index]);
			bitmap.x = -Math.round(bitmap.width / 2);
			bitmap.y = -Math.round(bitmap.height / 2);
			addChild(bitmap);
		}
		
		public function show():void
		{
			visible = true;
		}
		
		public function hide():void
		{
			visible = false;
		}
	}
	
}

internal class SingletonEnforcer {}